<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 10] Object</TITLE>
<META NAME="author" CONTENT="Mark Grand">
<META NAME="date" CONTENT="Fri Aug  8 15:25:52 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="java">
<META NAME="title" CONTENT="Java Language Reference">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java Language Reference" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch10_13.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 10<br>The java.lang Package</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch10_15.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=refnamediv>
<H1>Object</H1>

<H2>Name</H2>

Object

</DIV>

<DIV CLASS=refsect1>
<h2 CLASS=refsect1><A CLASS="TITLE" NAME="ch10-REFSECT1-AUTOID.103">Synopsis</A></h2>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Class Name:<br>
<DD>

<P CLASS=para>
<tt CLASS=literal>java.lang.Object</tt>

<p>
<DT CLASS=varlistentry>Superclass:<br>
<DD>

<P CLASS=para>
None

<p>
<DT CLASS=varlistentry>Immediate Subclasses:<br>
<DD>

<P CLASS=para>
Too many to be listed here

<p>
<DT CLASS=varlistentry>Interfaces Implemented:<br>
<DD>

<P CLASS=para>
None

<p>
<DT CLASS=varlistentry>Availability:<br>
<DD>

<P CLASS=para>
JDK 1.0 or later</DL>
</DIV>

<DIV CLASS=refsect1>
<h2 CLASS=refsect1><A CLASS="TITLE" NAME="ch10-REFSECT1-AUTOID.104">Description</A></h2>

<P CLASS=para>
<A NAME="MAN.OBJECT.1"></A>The <tt CLASS=literal>Object</tt> class is the ultimate superclass
of all other classes in Java. Because every other class is a subclass
of <tt CLASS=literal>Object</tt>, all of the methods accessible from
<tt CLASS=literal>Object</tt> are inherited by every other class. In
other words, all objects in Java, including arrays, have access
to implementations of the methods in <tt CLASS=literal>Object</tt>.

<P CLASS=para>
The methods of <tt CLASS=literal>Object</tt> provide some basic
object functionality. The <tt CLASS=literal>equals()</tt> method compares
two objects for equality, while the <tt CLASS=literal>hashCode()</tt>
method returns a hashcode for an object. The <tt CLASS=literal>getClass()</tt>
method returns the <tt CLASS=literal>Class</tt> object associated with
an object. The <tt CLASS=literal>wait()</tt>, <tt CLASS=literal>notify()</tt>,
and <tt CLASS=literal>notifyAll()</tt> methods support thread synchronization
for an object. The <tt CLASS=literal>toString()</tt> method provides
a string representation of an object.

<P CLASS=para>
Some of these methods should be overridden by subclasses of
<tt CLASS=literal>Object</tt>. For example, every class should provide
its own implementation of the <tt CLASS=literal>toString()</tt> method,
so that it can provide an appropriate string representation.

<P CLASS=para>
Although it is possible to create an instance of the <tt CLASS=literal>Object</tt>
class, this is rarely done because it is more useful to create specialized
objects. However, it is often useful to declare a variable that contains
a reference to an <tt CLASS=literal>Object</tt> because such a variable
can contain a reference to an object of any other class.

</DIV>

<DIV CLASS=refsect1>
<h2 CLASS=refsect1><A CLASS="TITLE" NAME="ch10-REFSECT1-AUTOID.105">Class Summary</A></h2>

<DIV CLASS=screen>
<P>
<PRE>
public class java.lang.Object {
    // Constructors
    public Object();
    // Public Instance Methods
    public boolean equals(Object obj);
    public final native Class getClass();
    public native int hashCode();
    public final native void notify();
    public final native void notifyAll();
    public String toString();
    public final native void wait();
    public final native void wait(long millis);
    public final native void wait(long millis, int nanos);
    // Protected Instance Methods
    protected native Object clone();
    protected void finalize() throws Throwable;
}
</PRE>
</DIV>

</DIV>

<DIV CLASS=refsect1>
<h2 CLASS=refsect1><A CLASS="TITLE" NAME="ch10-REFSECT1-AUTOID.106">Constructors</A></h2>

<DIV CLASS=refsect2>
<h3 CLASS=refsect2><A CLASS="TITLE" NAME="ch10-REFSECT2-AUTOID.257">Object</A></h3>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch10-REFSECT3-AUTOID.287"><tt CLASS=literal>public Object()</tt></A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
Creates an instance of the <tt CLASS=literal>Object</tt> class.</DL>
</DIV>

</DIV>

</DIV>

<DIV CLASS=refsect1>
<h2 CLASS=refsect1><A CLASS="TITLE" NAME="ch10-REFSECT1-AUTOID.107">Public Instance Methods</A></h2>

<DIV CLASS=refsect2>
<h3 CLASS=refsect2><A CLASS="TITLE" NAME="ch10-REFSECT2-AUTOID.258">equals</A></h3>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch10-REFSECT3-AUTOID.288"><tt CLASS=literal>public boolean equals(Object obj)</tt></A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Parameters<br>
<DD>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><tt CLASS=literal>obj</tt><br>
<DD>

<P CLASS=para>
The object to be compared with this object.</DL>
<p>
<DT CLASS=varlistentry>Returns<br>
<DD>

<P CLASS=para>
<tt CLASS=literal>true</tt> if the objects are equal; <tt CLASS=literal>false</tt>
if they are not.

<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>equals()</tt> method of <tt CLASS=literal>Object</tt>
returns <tt CLASS=literal>true</tt> if the <tt CLASS=literal>obj</tt> parameter
refers to the same object as the object this method is associated
with. This is equivalent to using the <tt CLASS=literal>==</tt> operator
to compare two objects.

<P CLASS=para>
Some classes, such as <tt CLASS=literal>String</tt>, override
the <tt CLASS=literal>equals()</tt> method to provide a comparison based
on the contents of the two objects, rather than on the strict equality
of the references. Any subclass can override the <tt CLASS=literal>equals()</tt>
method to implement an appropriate comparison, as long as the overriding
method satisfies the following rules for an equivalence relation:

<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>The method is <I CLASS=emphasis>reflexive</I> :
given a reference <tt CLASS=literal>x</tt>, <tt CLASS=literal>x.equals(x)</tt>
returns <tt CLASS=literal>true</tt>.

<P>
<li CLASS=listitem>The method is <I CLASS=emphasis>symmetric</I> : given
references <tt CLASS=literal>x</tt> and <tt CLASS=literal>y</tt>, <tt CLASS=literal>x.equals(y)</tt>
returns <tt CLASS=literal>true</tt> if and only if <tt CLASS=literal>y.equals(x)</tt>
returns <tt CLASS=literal>true</tt>.

<P>
<li CLASS=listitem>The method is <I CLASS=emphasis>transitive</I> : given
references <tt CLASS=literal>x</tt>, <tt CLASS=literal>y</tt>, and <tt CLASS=literal>z</tt>,
if <tt CLASS=literal>x.equals(y)</tt> returns <tt CLASS=literal>true</tt>
and <tt CLASS=literal>y.equals(z)</tt> returns <tt CLASS=literal>true</tt>,
then <tt CLASS=literal>x.equals(z)</tt> returns <tt CLASS=literal>true</tt>.
<P>
<li CLASS=listitem>The method is <I CLASS=emphasis>consistent</I> : given
references <tt CLASS=literal>x</tt> and <tt CLASS=literal>y</tt>, multiple
invocations of <tt CLASS=literal>x.equals(y)</tt> consistently return
<tt CLASS=literal>true</tt> or consistently return <tt CLASS=literal>false</tt>,
provided that no information contained by the objects referenced
by <tt CLASS=literal>x</tt> or <tt CLASS=literal>y</tt> changes.

<P>
<li CLASS=listitem>A comparison with <tt CLASS=literal>null</tt> returns
<tt CLASS=literal>false</tt>: given a reference <tt CLASS=literal>x</tt>
that is non-<tt CLASS=literal>null</tt>, <tt CLASS=literal>x.equals(null)</tt>
returns <tt CLASS=literal>false</tt>.

<P>
</UL>
</DL>
</DIV>

</DIV>

<DIV CLASS=refsect2>
<h3 CLASS=refsect2><A CLASS="TITLE" NAME="ch10-REFSECT2-AUTOID.259">getClass</A></h3>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch10-REFSECT3-AUTOID.289"><tt CLASS=literal>public final native Class getClass()</tt></A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Returns<br>
<DD>

<P CLASS=para>
A reference to the <tt CLASS=literal>Class</tt> object that describes
the class of this object.

<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getClass()</tt> method of <tt CLASS=literal>Object</tt>
returns the <tt CLASS=literal>Class</tt> object that describes the class
of this object. This method is <tt CLASS=literal>final</tt>, so it cannot
be overridden by subclasses.</DL>
</DIV>

</DIV>

<DIV CLASS=refsect2>
<h3 CLASS=refsect2><A CLASS="TITLE" NAME="ch10-REFSECT2-AUTOID.260">hashCode</A></h3>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch10-REFSECT3-AUTOID.290"><tt CLASS=literal>public native int hashCode()</tt></A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Returns<br>
<DD>

<P CLASS=para>
A relatively unique value that should be the same for all objects that
are considered equal.

<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>hashCode()</tt> method of <tt CLASS=literal>Object</tt>
calculates a hashcode value for this object. The method returns
an integer value that should be relatively unique to the object.
If the <tt CLASS=literal>equals()</tt> method for the object bases its
result on the contents of the object, the
<tt CLASS=literal>hashcode()</tt> method should also base its result on the
contents.
The <tt CLASS=literal>hashCode()</tt>
method is provided for the benefit of hashtables, which store and
retrieve elements using key values called <I CLASS=emphasis>hashcodes</I>.
The internal placement of a particular piece of data is determined
by its hashcode; hashtables are designed to use hashcodes to provide
efficient retrieval.

<P CLASS=para>
The <tt CLASS=literal>java.util.Hashtable</tt> class provides
an implementation of a hashtable that stores values of
type <tt CLASS=literal>Object</tt>.
Each object is stored in the hashtable based on the hash code of
its key object. It is important that each object have the most unique
hash code possible. If two objects have the same hash code but they
are not equal (as determined by <tt CLASS=literal>equals()</tt>), a
<tt CLASS=literal>Hashtable</tt> that stores these two objects may need to
spend additional time searching when it is trying to retrieve objects.
The implementation of <tt CLASS=literal>hashCode()</tt> in
<tt CLASS=literal>Object</tt> tries to make sure that every object has
a distinct hash code by basing its result on the internal
representation of a reference to the object.

<P CLASS=para>
Some classes, such as <tt CLASS=literal>String</tt>, override
the <tt CLASS=literal>hashCode()</tt> method to produce values based
on the contents of individual objects, instead of the objects themselves.
In other words, two <tt CLASS=literal>String</tt> objects that contain
the exact same strings have the same hash code. If <tt CLASS=literal>String</tt>
did not override the <tt CLASS=literal>hashCode()</tt> method inherited
from <tt CLASS=literal>Object</tt>, these two <tt CLASS=literal>String</tt>
objects would have different hash code values and it would be impossible
to use <tt CLASS=literal>String</tt> objects as keys for hashtables.
<P CLASS=para>
Any subclass can override the <tt CLASS=literal>hashCode()</tt>
method to implement an appropriate way of producing hash code values,
as long as the overriding method satisfies the following rules:

<P>
<UL CLASS=itemizedlist>
<li CLASS=listitem>If the <tt CLASS=literal>hashCode()</tt>
method is called on the same object more than once during the execution
of a Java application, it must consistently return the same integer
value. The integer does not, however, need to be consistent between
Java applications, or from one execution of an application to another
execution of the same application.

<P>
<li CLASS=listitem>If two objects compare as equal according to their
<tt CLASS=literal>equals()</tt> methods, calls to the <tt CLASS=literal>hashCode()</tt>
methods for the objects must produce the same result.

<P>
<li CLASS=listitem>If two objects compare as not equal according to
their <tt CLASS=literal>equals()</tt> methods, calls to the <tt CLASS=literal>hashCode()</tt>
methods for the two objects are not required to produce distinct
results. However, implementations of <tt CLASS=literal>hashCode()</tt>
that produce distinct results for unequal objects may improve the
performance of hashtables.

<P>
</UL>
<P CLASS=para>
In general, if a subclass overrides the <tt CLASS=literal>equals()</tt>
method of <tt CLASS=literal>Object</tt>, it should also override
the <tt CLASS=literal>hashCode()</tt> method.</DL>
</DIV>

</DIV>

<DIV CLASS=refsect2>
<h3 CLASS=refsect2><A CLASS="TITLE" NAME="ch10-REFSECT2-AUTOID.261">notify</A></h3>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch10-REFSECT3-AUTOID.291"><tt CLASS=literal>public final native void notify()</tt></A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Throws<br>
<DD>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><tt CLASS=literal>IllegalMonitorStateException</tt><br>
<DD>

<P CLASS=para>
If the method is called from a thread that does
not hold this object's lock.</DL>
<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>notify()</tt> method wakes up a thread that
is waiting to return from a call to this object's <tt CLASS=literal>wait()</tt>
method. The awakened thread can resume executing as soon as it regains
this object's lock. If more than one thread is waiting, the
<tt CLASS=literal>notify()</tt> method arbitrarily awakens just one
of the threads.

<P CLASS=para>
The <tt CLASS=literal>notify()</tt> method can be called only by a thread
that is the current owner of this object's lock. A thread holds the
lock on this object while it is executing a
<tt CLASS=literal>synchronized</tt> instance method of the object or
executing the body of a <tt CLASS=literal>synchronized</tt> statement that
synchronizes on the object. A thread can also hold the lock for a
<tt CLASS=literal>Class</tt> object if it is executing a
<tt CLASS=literal>synchronized</tt> <tt CLASS=literal>static</tt> method of
that class.

<P CLASS=para>
This method is <tt CLASS=literal>final</tt>, so it cannot be overridden
by subclasses.</DL>
</DIV>

</DIV>

<DIV CLASS=refsect2>
<h3 CLASS=refsect2><A CLASS="TITLE" NAME="ch10-REFSECT2-AUTOID.262">notifyAll</A></h3>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch10-REFSECT3-AUTOID.292"><tt CLASS=literal>public final native void notifyAll()</tt></A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Throws<br>
<DD>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><tt CLASS=literal>IllegalMonitorStateException</tt><br>
<DD>

<P CLASS=para>
If the method is called from a thread that does
not hold this object's lock.</DL>
<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>notifyAll()</tt> method wakes up all the
threads that are waiting to return from a call to this object's
<tt CLASS=literal>wait()</tt> method. Each awakened thread can resume
executing as soon as it regains this object's lock.

<P CLASS=para>
The <tt CLASS=literal>notifyAll()</tt> method can be called only by a
thread that is the current owner of this object's lock. A thread holds
the lock on this object while it is executing a
<tt CLASS=literal>synchronized</tt> instance method of the object or
executing the body of a <tt CLASS=literal>synchronized</tt> statement that
synchronizes on the object. A thread can also hold the lock for a
<tt CLASS=literal>Class</tt> object if it is executing a
<tt CLASS=literal>synchronized</tt> <tt CLASS=literal>static</tt> method of
that class.

<P CLASS=para>
This method is <tt CLASS=literal>final</tt>, so it cannot be overridden
by subclasses.</DL>
</DIV>

</DIV>

<DIV CLASS=refsect2>
<h3 CLASS=refsect2><A CLASS="TITLE" NAME="ch10-REFSECT2-AUTOID.263">toString</A></h3>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch10-REFSECT3-AUTOID.293"><tt CLASS=literal>public String toString()</tt></A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Returns<br>
<DD>

<P CLASS=para>
The string representation of this object.

<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>toString()</tt> method of <tt CLASS=literal>Object</tt> returns a generic string representation of this object.
The method returns a <tt CLASS=literal>String</tt> that consists of
the object's class name, an "at" sign, and the unsigned hexadecimal
representation of the value returned by the object's <tt CLASS=literal>hashCode()</tt>
method.

<P CLASS=para>
Many classes override the <tt CLASS=literal>toString()</tt> method
to provide a string representation that is specific to that type
of object. Any subclass can override the <tt CLASS=literal>toString()</tt> method; the
overriding method should simply return a <tt CLASS=literal>String</tt> that represents
the contents of the object with which it is associated.</DL>
</DIV>

</DIV>

<DIV CLASS=refsect2>
<h3 CLASS=refsect2><A CLASS="TITLE" NAME="ch10-REFSECT2-AUTOID.264">wait</A></h3>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch10-REFSECT3-AUTOID.294"><tt CLASS=literal>public final native void wait() throws InterruptedException</tt></A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Throws<br>
<DD>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><tt CLASS=literal>IllegalMonitorStateException</tt><br>
<DD>

<P CLASS=para>
If the method is called from a thread that does
not hold this object's lock.

<p>
<DT CLASS=varlistentry><tt CLASS=literal>InterruptedException</tt><br>
<DD>

<P CLASS=para>
If another thread interrupted this thread.</DL>
<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>wait()</tt> method causes a thread to wait
until it is notified by another thread to stop waiting.
When <tt CLASS=literal>wait()</tt> is called, the thread releases its
lock on this object and waits until another thread notifies it to
wake up through a call to either <tt CLASS=literal>notify()</tt> or
<tt CLASS=literal>notifyAll()</tt>. After the thread is awakened, it
has to regain the lock on this object before it can resume executing.

<P CLASS=para>
The <tt CLASS=literal>wait()</tt> method can be called only by
a thread that is the current owner of this object's lock. A thread
holds the lock on this object while it is executing a <tt CLASS=literal>synchronized</tt>
instance method of the object or executing the body of a <tt CLASS=literal>synchronized</tt>
statement that synchronizes on the object. A thread can also hold
the lock for a <tt CLASS=literal>Class</tt> object if it is executing
a <tt CLASS=literal>synchronized</tt> <tt CLASS=literal>static</tt> method
of that class.

<P CLASS=para>
This method is <tt CLASS=literal>final</tt>, so it cannot be overridden
by subclasses.</DL>
</DIV>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch10-REFSECT3-AUTOID.295"><DIV CLASS=screen> <P> <PRE> public final native void wait(long timeout) throws InterruptedException </PRE> </DIV></A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Parameters<br>
<DD>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><tt CLASS=literal>timeout</tt><br>
<DD>

<P CLASS=para>
The maximum number of milliseconds to wait.</DL>
<p>
<DT CLASS=varlistentry>Throws<br>
<DD>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><tt CLASS=literal>IllegalMonitorStateException</tt><br>
<DD>

<P CLASS=para>
If the method is called from a thread that does
not hold this object's lock.

<p>
<DT CLASS=varlistentry><tt CLASS=literal>InterruptedException</tt><br>
<DD>

<P CLASS=para>
If another thread interrupted this thread.</DL>
<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>wait()</tt> method causes a thread to wait
until it is notified by another thread to stop waiting
or until the specified amount of time has elapsed, whichever comes
first. When <tt CLASS=literal>wait()</tt> is called, the thread releases
its lock on this object and waits until another thread notifies
it to wake up through a call to either <tt CLASS=literal>notify()</tt>
or <tt CLASS=literal>notifyAll()</tt>. If the thread is not notified
within the specified <tt CLASS=literal>timeout</tt> period, it is automatically
awakened when that amount of time has elapsed. If <tt CLASS=literal>timeout</tt>
is zero, the thread waits indefinitely, just as if <tt CLASS=literal>wait()</tt>
had been called without a <tt CLASS=literal>timeout</tt> argument. After
the thread is awakened, it has to regain the lock on this object
before it can resume executing.

<P CLASS=para>
The <tt CLASS=literal>wait()</tt> method can be called only by
a thread that is the current owner of this object's lock. A thread
holds the lock on this object while it is executing a <tt CLASS=literal>synchronized</tt>
instance method of the object or executing the body of a <tt CLASS=literal>synchronized</tt>
statement that synchronizes on the object. A thread can also hold
the lock for a <tt CLASS=literal>Class</tt> object if it is executing
a <tt CLASS=literal>synchronized</tt> <tt CLASS=literal>static</tt> method
of that class.

<P CLASS=para>
This method is <tt CLASS=literal>final</tt>, so it cannot be overridden
by subclasses.</DL>
</DIV>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch10-REFSECT3-AUTOID.296"><DIV CLASS=screen> <P> <PRE> public final native void wait(long timeout, int nanos) throws InterruptedException </PRE> </DIV></A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Parameters<br>
<DD>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><tt CLASS=literal>timeout</tt><br>
<DD>

<P CLASS=para>
The maximum number of milliseconds to wait.

<p>
<DT CLASS=varlistentry><tt CLASS=literal>nanos</tt><br>
<DD>

<P CLASS=para>
An additional number of nanoseconds to wait.</DL>
<p>
<DT CLASS=varlistentry>Throws<br>
<DD>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><tt CLASS=literal>IllegalMonitorStateException</tt><br>
<DD>

<P CLASS=para>
If the method is called from a thread that does
not hold this object's lock.

<p>
<DT CLASS=varlistentry><tt CLASS=literal>InterruptedException</tt><br>
<DD>

<P CLASS=para>
If another thread interrupted this thread.</DL>
<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>wait()</tt> method causes a thread to wait
until it is notified by another thread to stop waiting
or until the specified amount of time has elapsed, whichever comes
first. When <tt CLASS=literal>wait()</tt> is called, the thread releases
its lock on this object and waits until another thread notifies
it to wake up through a call to either <tt CLASS=literal>notify()</tt>
or <tt CLASS=literal>notifyAll()</tt>. If the thread is not notified
within the specified time period, it is automatically awakened when
that amount of time has elapsed. If <tt CLASS=literal>timeout</tt> and
<tt CLASS=literal>nanos</tt> are zero, the thread waits indefinitely,
just as if <tt CLASS=literal>wait()</tt> had been called without any
arguments. After the thread is awakened, it has to regain the lock
on this object before it can resume executing.

<P CLASS=para>
The <tt CLASS=literal>wait()</tt> method can be called only by
a thread that is the current owner of this object's lock. A thread
holds the lock on this object while it is executing a <tt CLASS=literal>synchronized</tt>
instance method of the object or executing the body of a <tt CLASS=literal>synchronized</tt>
statement that synchronizes on the object. A thread can also hold
the lock for a <tt CLASS=literal>Class</tt> object if it is executing
a <tt CLASS=literal>synchronized</tt> <tt CLASS=literal>static</tt> method
of that class.

<P CLASS=para>
Note that Sun's reference implementation
of Java does not attempt to implement the precision implied by this
method. Instead, it rounds to the nearest millisecond (unless <tt CLASS=literal>timeout</tt>
is <tt CLASS=literal>0</tt>, in which case it rounds up to 1 millisecond)
and calls <tt CLASS=literal>wait(long)</tt>.

<P CLASS=para>
This method is <tt CLASS=literal>final</tt>, so it cannot be overridden
by subclasses.</DL>
</DIV>

</DIV>

</DIV>

<DIV CLASS=refsect1>
<h2 CLASS=refsect1><A CLASS="TITLE" NAME="ch10-REFSECT1-AUTOID.108">Protected Instance Methods</A></h2>

<DIV CLASS=refsect2>
<h3 CLASS=refsect2><A CLASS="TITLE" NAME="ch10-REFSECT2-AUTOID.265">clone</A></h3>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch10-REFSECT3-AUTOID.297"><tt CLASS=literal>protected native Object clone() throws CloneNotSupportedException</tt></A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Returns<br>
<DD>

<P CLASS=para>
A clone of this object.

<p>
<DT CLASS=varlistentry>Throws<br>
<DD>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><tt CLASS=literal>OutOfMemoryError</tt><br>
<DD>

<P CLASS=para>
If there is not enough memory to create the new
object.

<p>
<DT CLASS=varlistentry><tt CLASS=literal>CloneNotSupportedException</tt><br>
<DD>

<P CLASS=para>
If the object is of a class that does not support
<tt CLASS=literal>clone()</tt>.</DL>
<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
A <I CLASS=emphasis>clone</I>
of an object is another object of the same type that
has all of its instance variables set to the same values as the
object being cloned. In other words, a clone is an exact copy of
the original object.

<P CLASS=para>
The <tt CLASS=literal>clone()</tt> method of <tt CLASS=literal>Object</tt>
creates a new object that is a clone of this object. No constructor
is used in creating the clone. The <tt CLASS=literal>clone()</tt> method
only clones an object if the class of that object indicates that
its instances can be cloned. A class indicates that its objects
can be cloned by implementing the <tt CLASS=literal>Cloneable</tt> interface.
<P CLASS=para>
Although array objects do not implement the
<tt CLASS=literal>Cloneable</tt> interface, the <tt CLASS=literal>clone()</tt>
method works for arrays. The clone of an array is an array that has
the same number of elements as the original array, and each element in
the clone array has the same value as the corresponding element in the
original array. Note that if an array element contains an object
reference, the clone array contains a reference to the same object,
not a copy of the object.

<P CLASS=para>
A subclass of <tt CLASS=literal>Object</tt> can override the <tt CLASS=literal>clone()</tt>
method in <tt CLASS=literal>Object</tt> to provide any additional functionality
that is needed. For example, if an object contains references to
other objects, the <tt CLASS=literal>clone()</tt> method should recursively
call the <tt CLASS=literal>clone()</tt> methods of all the referenced
objects. An overriding <tt CLASS=literal>clone()</tt> method can throw
a <tt CLASS=literal>CloneNotSupportedException</tt> to indicate that
particular objects cannot be cloned.</DL>
</DIV>

</DIV>

<DIV CLASS=refsect2>
<h3 CLASS=refsect2><A CLASS="TITLE" NAME="ch10-REFSECT2-AUTOID.266">finalize</A></h3>

<DIV CLASS=refsect3>
<h4 CLASS=refsect3><A CLASS="TITLE" NAME="ch10-REFSECT3-AUTOID.298"><tt CLASS=literal>protected void finalize() throws Throwable</tt></A></h4>

<DL CLASS=variablelist>
<DT CLASS=varlistentry>Throws<br>
<DD>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><tt CLASS=literal>Throwable</tt><br>
<DD>

<P CLASS=para>
For any reason that suits an overriding implementation
of this method.</DL>
<p>
<DT CLASS=varlistentry>Description<br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>finalize()</tt> method is called by the
garbage collector when it decides that an object can never be referenced
again. The method gives an object a chance to perform any cleanup
operations that are necessary before it is destroyed by the garbage
collector.

<P CLASS=para>
The <tt CLASS=literal>finalize()</tt> method of <tt CLASS=literal>Object</tt>
does nothing. A subclass overrides the <tt CLASS=literal>finalize()</tt>
method to perform any necessary cleanup operations. The overriding
method should call <tt CLASS=literal>super.finalize()</tt> as the very
last thing it does, so that any <tt CLASS=literal>finalize()</tt> method
in the superclass is called.

<P CLASS=para>
When the garbage collector calls an object's <tt CLASS=literal>finalize()</tt>
method, the garbage collector does not immediately destroy the object
because the <tt CLASS=literal>finalize()</tt> method might do something
that results in a reference to the object. Thus the garbage collector
waits to destroy the object until it can again prove it is safe
to do so. The next time the garbage collector decides it is safe
to destroy the object, it does so without calling <tt CLASS=literal>finalize()</tt>
again. In other words, the garbage collector never calls the
<tt CLASS=literal>finalize()</tt> method more than once for a particular object.

<P CLASS=para>
A <tt CLASS=literal>finalize()</tt> method can throw any kind
of exception. An exception causes the <tt CLASS=literal>finalize()</tt>
method to stop running. The garbage collector then catches and ignores
the exception, so it has no further effect on a program.</DL>
</DIV>

</DIV>

</DIV>

<DIV CLASS=refsect1>
<h2 CLASS=refsect1><A CLASS="TITLE" NAME="ch10-REFSECT1-AUTOID.109">See Also</A></h2>

<P CLASS=para>
<A HREF="ch04_09.htm#JLR2-CH-4-SECT-9">Equality Comparison Operators</A>;
<A HREF="ch09_04.htm#JLR2-CH-9-SECT-4.1">Exceptions</A>;
<A HREF="ch05_03.htm#JLR2-CH-5-SECT-3.4">Object Destruction</A>;
<A HREF="ch05_04.htm#JLR2-CH-5-SECT-4.4.2.7">The finalize method</A>;
<A HREF="ch10_20.htm">String</A>;
Threads 8;
<A HREF="ch10_25.htm">Throwable</A>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch10_13.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch10_15.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Number</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Process</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
